package com.bocaizp.operation.service.impl;

import com.bocaizp.common.result.PageResult;
import com.bocaizp.operation.mapper.QueryJobMapper;
import com.bocaizp.operation.service.QueryJobService;
import com.bocaizp.pojo.DTO.JobDataDTO;
import com.bocaizp.pojo.VO.HotJobVO;
import com.bocaizp.pojo.VO.JobDataVO;
import com.bocaizp.pojo.entity.JobData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Author : huang jie
 * Date : 2024/5/3 23:09
 **/
@Service
@Slf4j
public class QueryJobServiceImpl implements QueryJobService {

	@Autowired
	private QueryJobMapper queryJobMapper;

	/**
	 * 推荐职位展示
	 * @param jobDataDTO
	 * @return
	 */
	public PageResult jobRecommend(JobDataDTO jobDataDTO) {
		PageHelper.startPage(jobDataDTO.getPage(), jobDataDTO.getPageSize());

		Long start = System.currentTimeMillis();
		Page<Long> ids = queryJobMapper.getjob(jobDataDTO);
		if(ids.getTotal() == 0){
			return new PageResult(0,null);
		}
		List<JobData> jobDataVOS = queryJobMapper.Jobs(ids.getResult());
		Long end = System.currentTimeMillis();
		System.out.println("职位请求时间：" + (end - start) + "毫秒");

		List<JobDataVO> jobDataVOS1 = new ArrayList<>();
		for (JobData jobData : jobDataVOS){

			JobDataVO jobDataVO = new JobDataVO();
			BeanUtils.copyProperties(jobData, jobDataVO);
			String salary = backSalary(jobData.getSalaryBottom(), jobData.getSalaryTop(), jobData.getSalaryMonth());
			jobDataVO.setSalary(salary);
			jobDataVOS1.add(jobDataVO);
		}
		return new PageResult(ids.getTotal(), jobDataVOS1);
	}

	/**
	 * 热招职位
	 * @param job
	 * @return
	 */
	public List<HotJobVO> gethotJob(String job) {
		List<JobData> jobData = queryJobMapper.getHot(job);
		List<HotJobVO> hotJobVOS = new ArrayList<>();

		for (JobData jobData1 : jobData){
			HotJobVO hotJobVO = new HotJobVO();
			BeanUtils.copyProperties(jobData1, hotJobVO);
			String salary = backSalary(jobData1.getSalaryBottom(), jobData1.getSalaryTop(), jobData1.getSalaryMonth());
			hotJobVO.setSalary(salary);
			hotJobVOS.add(hotJobVO);
		}
		return hotJobVOS;
	}

	/**
	 * 增加热度
	 * @param id
	 */
	public void addHot(Long id) {
		int hot = queryJobMapper.selectHot(id);
		int newHot = hot + 1;
		queryJobMapper.updateHot(id, newHot);
	}


	public String backSalary(int salary_bottom, int salary_top, int salary_month){
		String s_bottom = salary_bottom / 1000 + "";
		String s_top = salary_top / 1000 + "";
		String s_month = "";
		if(salary_month != 12){
			s_month = "·" + salary_month + "薪";
		}
		return s_bottom + "-" +s_top + "K" + s_month;
	}
}
